<template>
  <view
    class="card_rolls_box"
    @touchmove="refresh.moveTouchendHandler"
    @touchstart="refresh.startTouchendHandler"
    @touchend="refresh.endTouchendHandler"
  >
    <menu-navigation-bar
      :is-sticky="true"
      :menuList="menuList"
      @on-selected="onMenuSelected"
    />

    <view class="content">
      <card-rolls-item
        v-for="(item, index) in dataList"
        :key="index"
        :data="item"
      />
    </view>

    <!-- 加载状态 -->
    <load :status="loadStatue" :isImage="dataList.length < 1" />
  </view>

  <!-- 下拉刷新 -->
  <pull-down-refresh
    :opacity="refresh.moveBottom.value / 3"
    :enble-anim="refresh.isRefresh.value"
  />
</template>

<!-- 卡卷页面 -->
<script lang="ts" setup>
import cardRollsItem from './components/card-rolls-item.vue'
import { refreshHandler } from '@/components/common/pull_down_refresh/index'
import { ReqSelectCardRollsPageByState } from '@/api/cardRolls'
import menuNavigationBar from '@/components/layout/menu_navigation_bar/menu-navigation-bar.vue'
import type {
  PageCardRollsByState,
  QueryPageCardRollsByState,
} from '@/config/data/data'
import { CardRollsStateEnum } from '@/config/enum/enum'
import { onLoad } from '@dcloudio/uni-app'
import { ref, watch } from 'vue'

//菜单列表数据
const menuList = ref<string[]>([])

//加载状态
const loadStatue = ref(-1)

//防止重复点击
const isReq = ref(true)

//下拉刷新控制函数
const refresh = refreshHandler(300)

//数据列表
const dataList = ref<PageCardRollsByState[]>([])

//菜单变更
const onMenuSelected = (index: number) => {
  queryPage.value.page = 0
  queryPage.value.state = menuList.value[index]
  nextData(true)
}

//初始化数据
const initData = () => {
  menuList.value.push(CardRollsStateEnum.UNUSER)
  menuList.value.push(CardRollsStateEnum.USED)
  menuList.value.push(CardRollsStateEnum.EXPIRED)
  queryPage.value.state = menuList.value[0]
  nextData()
}

//监听下拉刷新状态
watch(
  () => refresh.isRefresh.value,
  (newValue, oldValue) => {
    if (newValue) {
      //开启刷新，并请求
      queryPage.value.page = 0
      nextData(true).finally(() => {
        refresh.refreshEnd(500)
      })
    }
  }
)

//请求参数
const queryPage = ref<QueryPageCardRollsByState>({
  page: 0,
  size: 10,
  state: '',
})

/**
 * 下一页
 * @param refresh 是否刷新
 * @return 返回一个Promise标识请求结束
 */
const nextData = (refresh: boolean = false) => {
  return new Promise((reslove) => {
    if (!isReq.value) {
      reslove(true)
      return
    }
    loadStatue.value = 0
    queryPage.value.page += 1
    isReq.value = false
    ReqSelectCardRollsPageByState(queryPage.value)
      .then((res) => {
        if (res.code == 200 && res.data) {
          refresh ? (dataList.value = []) : ''
          dataList.value.push(...res.data)
          let bool = queryPage.value.size == res.data.length
          loadStatue.value = bool ? 1 : -1
        }
      })
      .catch(() => {
        loadStatue.value = -2
        setTimeout(() => {
          loadStatue.value = -1
        }, 100)
      })
      .finally(() => {
        isReq.value = true
        reslove(true)
      })
  })
}

//页面加载
onLoad(() => {
  initData()
})
</script>

<style scoped lang="scss">
.card_rolls_box {
  width: 100%;
  min-height: 100vh;
  background-color: $uni-bg-color-grey;

  .content {
    padding: 0 30rpx;
    width: calc(100% - 60rpx);
  }
}
</style>
